package com.fjl.controller;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.audio.tts.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.tts.SpeechSynthesizer;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Controller
public class StreamController {


    @Value("${ali.bailian.api-key}")
    private String apiKey;
    @Value("${ali.bailian.text.to.img.model}")
    private String textToImgModel;
    @Value("${ali.bailian.text.to.audio.model}")
    private String textToAudioModel;

    @Autowired
    private ChatModel chatModel;
    @Autowired
    private ChatClient chatClient;


    /**
     * 使用 ChatModel 的流式输出
     */
    @CrossOrigin(origins = "*")
    @GetMapping(value = "/chatModel/flux/chat", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chatModelFluxChat(@RequestParam(name = "msg")String msg){
        return chatModel.stream(msg);
    }

    /**
     * 使用 ChatClient 的流式输出
     */
    @CrossOrigin(origins = "*")
    @GetMapping(value = "/chatClient/flux/chat", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chatClientFluxChat(@RequestParam(name = "msg")String msg){
        return chatClient.prompt(msg).stream().content();
    }

}